FcText

object FcText

Various text utilities and wrappers for making kotlin minecraft modding more text-expressive

Author

fzzyhmstrs

Since

0.2.0

See also

Samples

import com.mojang.brigadier.LiteralMessage
import me.fzzyhmstrs.fzzy_config.fcId
import me.fzzyhmstrs.fzzy_config.util.FcText
import me.fzzyhmstrs.fzzy_config.util.FcText.bold
import me.fzzyhmstrs.fzzy_config.util.FcText.command
import me.fzzyhmstrs.fzzy_config.util.FcText.descLit
import me.fzzyhmstrs.fzzy_config.util.FcText.lit
import me.fzzyhmstrs.fzzy_config.util.FcText.text
import me.fzzyhmstrs.fzzy_config.util.FcText.tooltip
import me.fzzyhmstrs.fzzy_config.util.FcText.transLit
import me.fzzyhmstrs.fzzy_config.util.FcText.translate
import me.fzzyhmstrs.fzzy_config.util.FcText.underline
import me.fzzyhmstrs.fzzy_config.util.Translatable
import net.minecraft.registry.RegistryKeys
import net.minecraft.registry.tag.TagKey
import net.minecraft.util.Identifier
import net.minecraft.util.math.ChunkPos
import java.util.*

fun main() { 
   //sampleStart 
   //FcText has wrappers for the standard Text methods, historically used for porting
val standardText = FcText.literal("Normal text")
val translateText = FcText.translatable("my.translatable.text")
val fallbackText = FcText.translatableWithFallback("my.translatable.text", "My Fallback")
val stringifiedText = FcText.stringified("my.stringified.text", TagKey.of(RegistryKeys.ITEM, "arg_requiring_stringification".fcId()))
val emptyText = FcText.empty()
val appendedText = FcText.appended(standardText, fallbackText)

//several extension functions for converting common MC and Java objects into text
val idText = Identifier.of("stick").text()
val uuidText = UUID.fromString("732bf411-5bb5-4f5d-8ef0-feb45d6032ee").text()
val dateText = Date().text()
val messageText = LiteralMessage("Example Message").text()
val chunkPosText = ChunkPos(4, 4).text()

//string extension functions for simple text-ification of string literals
val stringLit = "My Cool String".lit()
val stringTranslate = "my.cool.string".translate() // can add args too

// simple example class that implements Translatable
class TranslatableExample: Translatable {
    override fun translationKey(): String {
        return "example.translatable.translation"
    }
    override fun descriptionKey(): String {
        return "example.translatable.translation.desc"
    }
}

//provide translations and descriptions for anything, particularly hooking into Translatable
val myTranslatableThing = TranslatableExample()

// translates anything, first checking if the thing is Translatable and using that translation if found, otherwise it translates the fallback key
val anyTranslate = myTranslatableThing.translation("my.fallback.translation")
//translate anything, first checking if the thing is Translatable and using that translation if found, otherwise it provides the fallback string literally
val anyTransLit = myTranslatableThing.transLit("Fallback Message")

// describes anything, first checking if the thing is Translatable and using that description if found, otherwise it translates the fallback key
val anyDescription = myTranslatableThing.description("my.fallback.translation.desc")
//describes anything, first checking if the thing is Translatable and using that description if found, otherwise it provides the fallback string literally
val anyDescLit = myTranslatableThing.descLit("Fallback Description")

//easily add common styling and context actions with extensions
val myCoolText = "text.with.context".translate().bold().underline().tooltip("text.with.context.tooltip".translate()).command("/gamemode creative") 
   //sampleEnd
}

Functions

Link copied to clipboard
fun appended(baseText: MutableText, vararg appended: Text): MutableText

Appends multiple texts to a base text

Link copied to clipboard
fun MutableText.bold(): MutableText

Bolds the receiver text

Link copied to clipboard
fun MutableText.colored(color: Int): MutableText

Applies an RGB color to the receiver text

Link copied to clipboard
fun MutableText.command(command: String): MutableText

Adds a Run Command click action to a MutableText

Link copied to clipboard
fun Any?.descLit(literalFallback: String = ""): Text

Describes anything (In enchantment description style, or for tooltips, for example). If the thing is Translatable, it will use the built-in description, otherwise it will use the fallback literally

Link copied to clipboard
fun Any?.description(fallback: String): Text

Describes anything (In enchantment description style, or for tooltips, for example). If the thing is Translatable, it will use the built in description, otherwise it will translate the fallback key

Link copied to clipboard
fun Any?.descSupplied(fallbackSupplier: Supplier<String>): MutableText

Describes anything (In enchantment description style, or for tooltips, for example). If the thing is Translatable, it will use the built-in description, otherwise it will use the fallback literally

Link copied to clipboard
fun empty(): MutableText

Wrapper method around Text.empty. A backwards compatibility holdover from porting older versions

Link copied to clipboard
fun Text.isEmpty(): Boolean
Link copied to clipboard
fun Text.isNotEmpty(): Boolean
Link copied to clipboard
fun MutableText.italic(): MutableText

Italicizes the receiver text

Link copied to clipboard
fun String.lit(): MutableText

Extension function converts a string into a literal Text representing it

Link copied to clipboard
fun literal(text: String): MutableText

Wrapper method around Text.literal. A backwards compatibility holdover from porting older versions

Link copied to clipboard
fun MutableText.strikethrough(): MutableText

Strikes through the receiver text

Link copied to clipboard
fun stringified(key: String, vararg args: Any): MutableText

Wrapper method around Text.stringified. A backwards compatibility holdover from porting older versions

Link copied to clipboard
fun Message.text(): Text

Extension function for converting Messages into Texts in a kotlin style

fun Date.text(): Text

Extension function for converting Dates into Texts in a kotlin style

fun UUID.text(): Text

Extension function for converting UUIDs into Texts in a kotlin style

fun Identifier.text(): Text

Extension function for converting Identifiers into Texts in a kotlin style

fun ChunkPos.text(): Text

Extension function for converting ChunkPos into Texts in a kotlin style

Link copied to clipboard
fun toLinebreakText(texts: List<Text>): MutableText
Link copied to clipboard
fun MutableText.tooltip(tooltip: Text): MutableText

Adds a Show Text hover action (a tooltip) to a MutableText

Link copied to clipboard
fun translatable(key: String, vararg args: Any): MutableText

Wrapper method around Text.translatable. A backwards compatibility holdover from porting older versions

Link copied to clipboard
fun translatableWithFallback(key: String, fallback: String?, vararg args: Any): MutableText

Wrapper method around Text.translatableWithFallback. A backwards compatibility holdover from porting older versions

Link copied to clipboard
fun String.translate(vararg args: Any): MutableText

Extension function uses the receiver String as a translation key to convert it into Text

Link copied to clipboard
fun Any?.translation(fallback: String): MutableText

Translates anything. If the thing is Translatable, it will use the built-in translation, otherwise it will translate the fallback key

Link copied to clipboard
fun Any?.transLit(literalFallback: String = ""): MutableText

Translates anything. If the thing is Translatable, it will use the built in translation, otherwise it will use the fallback literally

Link copied to clipboard
fun Any?.transSupplied(fallbackSupplier: Supplier<String>): MutableText

Translates anything. If the thing is Translatable, it will use the built in translation, otherwise it will use the fallback literally

Link copied to clipboard
fun MutableText.underline(): MutableText

Underlines the receiver text